﻿package 
{
	import com.onebyonedesign.extras.VideoLoop; 
	import flash.display.Sprite; 
	import flash.events.AsyncErrorEvent; 

	import flash.events.Event; 
	import flash.events.MouseEvent; 
	import flash.text.AntiAliasType; 
	import flash.text.TextField; 
	import flash.text.TextFieldAutoSize; 
	import flash.text.TextFormat; 
	import flash.text.TextFormatAlign;  
 	import flash.external.ExternalInterface;
	
	///

    import flash.events.*;
    import flash.external.ExternalInterface;

    import flash.utils.Timer;
    import flash.text.TextFieldType;
    import flash.text.TextFieldAutoSize;
	///
	
	/**
	 * ...
	 * @author tom
	 */
	public class Main extends Sprite 
	{
		private var videoLoop:VideoLoop;  
		//
		private var input:TextField;
        private var output:TextField;
        private var sendBtn:Sprite;
		//
		
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			videoLoop = new VideoLoop("go.flv"); 
			videoLoop.addEventListener(AsyncErrorEvent.ASYNC_ERROR, onAsyncError); 

			addChild(videoLoop);  
 
			stage.addEventListener(MouseEvent.CLICK, adjustVideo);  
 
 			videoLoop.width=960;
			videoLoop.height=362;
 
			var info:TextField = new TextField();
			info.selectable = false;
			info.autoSize = TextFieldAutoSize.LEFT;
			info.antiAliasType = AntiAliasType.ADVANCED;
			var fmt:TextFormat = new TextFormat("_sans", 12, 0x939393);
			fmt.align = TextFormatAlign.CENTER;
			info.defaultTextFormat = fmt;
			info.text = "Two second video looped.\nClick to pause/resume.";
			info.x = 95;
			info.y = 190;
			addChild(info);
			info.visible = false;
			
			///
			input = new TextField();
            input.type = TextFieldType.INPUT;
            input.background = true;
            input.border = true;
            input.width = 350;
            input.height = 18;
            addChild(input);
			input.visible = false;

            sendBtn = new Sprite();
            sendBtn.mouseEnabled = true;
            sendBtn.x = input.width + 10;
            sendBtn.graphics.beginFill(0xCCCCCC);
            sendBtn.graphics.drawRoundRect(0, 0, 80, 18, 10, 10);
            sendBtn.graphics.endFill();
            sendBtn.addEventListener(MouseEvent.CLICK, clickHandler);
            addChild(sendBtn);
			sendBtn.visible = false;
            output = new TextField();
            output.y = 25;
            output.width = 450;
            output.height = 325;
            output.multiline = true;
            output.wordWrap = true;
            output.border = true;
            output.text = "Initializing...\n";
            addChild(output);
			output.visible = false;

            if (ExternalInterface.available) {
                try {
                    output.appendText("Adding callback...\n");
                    ExternalInterface.addCallback("sendToActionScript", receivedFromJavaScript);
                    if (checkJavaScriptReady()) {
                        output.appendText("JavaScript is ready.\n");
                    } else {
                        output.appendText("JavaScript is not ready, creating timer.\n");
                        var readyTimer:Timer = new Timer(100, 0);
                        readyTimer.addEventListener(TimerEvent.TIMER, timerHandler);
                        readyTimer.start();
                    }
                } catch (error:SecurityError) {
                    output.appendText("A SecurityError occurred: " + error.message + "\n");
                } catch (error:Error) {
                    output.appendText("An Error occurred: " + error.message + "\n");
                }
            } else {
                output.appendText("External interface is not available for this container.");
            }
			///
			
		}
		
		private function onAsyncError(aee:AsyncErrorEvent):void { 
			//	handle annoying async errors (such as the missing metadata property) here. 
		} 
		
		private function adjustVideo(me:MouseEvent):void { 
			//trace("click /"+ isPlaying);
			if (videoLoop.isPlaying) { 
				//videoLoop.pause(); 
			} else { 
				videoLoop.play(); 
			} 
			videoLoop.isPlaying = !videoLoop.isPlaying;
		}
		
		private function jsPlayVideo() {
			videoLoop.play(); 
			videoLoop.isPlaying = !videoLoop.isPlaying;
		}
		
		///
		
		private function receivedFromJavaScript(value:String):void {
           videoLoop.requiredCp = value;
			jsPlayVideo();

			//output.appendText("JavaScript says: " + value + "\n");
        }
        private function checkJavaScriptReady():Boolean {
            var isReady:Boolean = ExternalInterface.call("isReady");
            return isReady;
        }
        private function timerHandler(event:TimerEvent):void {
            output.appendText("Checking JavaScript status...\n");
            var isReady:Boolean = checkJavaScriptReady();
            if (isReady) {
                output.appendText("JavaScript is ready.\n");
                Timer(event.target).stop();
            }
        }
        private function clickHandler(event:MouseEvent):void {
            if (ExternalInterface.available) {
                ExternalInterface.call("sendToJavaScript", input.text);
            }
        }
		///
		
	}
	
}